package com.lxy.controller.User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxy.common.BaseContext;
import com.lxy.common.R;
import com.lxy.entity.AddressBook;
import com.lxy.service.AddressBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.Address;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址簿
     * @param addressBook 地址簿
     * @return
     */
    @ApiOperation("新增地址簿")
    @PostMapping("/save")
    public R save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新增地址成功");
    }

    /**
     * 删除地址簿
     * @param id 地址簿id
     * @return
     */
    @ApiOperation("删除地址簿")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        addressBookService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 修改地址簿
     * @param addressBook
     * @return
     */
    @ApiOperation("修改地址簿")
    @PostMapping("/update")
    public R update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


    /**
     * 查看自己的地址簿
     */
    @ApiOperation("查看自己的地址簿")
    @GetMapping("/showAddressBook")
    public R<List<AddressBook>> showAddressBook(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 根据id查找地址簿信息
     */
    @ApiOperation("根据id查找地址簿信息")
    @GetMapping("/getById/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);
    }

}
